uni.addInterceptor('request', {
	invoke(args) {
		
		if (args.url.includes("uni-app")) {
			// request 触发前拼接 url
			if (!args.url.includes('?')) {
				args.url = 'http://localhost:8081/' + args.url + "?token=" + uni.getStorageSync("token")
			} else {
				args.url = 'http://localhost:8081/' + args.url + "&token=" + uni.getStorageSync("token")
			}
		}
		if(args.url.includes("login")){
			args.url = 'http://localhost:8081/' + args.url
		}
		args.header = {
			...args.header,
			Authorization:`Bearer${uni.getStorageSync("token")}`
		};
		
	},
	success(args) {
		// 请求成功后，修改code值为1
		if(args.data.code == 401){
			setTimeout(function(){
				uni.showModal({
					title:'请登录',
					content:'您还未登录',
					success() {
						uni.reLaunch({
							url:'/pages/login/login'
						}) // 请求不成功时，拒绝 Promise
					}
				})
			},2000)
		}
		if( args.data.code == 403){
			setTimeout(function(){
				uni.showModal({
					title:'无效访问',
					content:'您没有权限'
				})
			},2000)
		}
		
	},
	fail(err) {
		console.log("11111111");
	}
});

uni.addInterceptor('navigateTo', {
	invoke(args) {
		if (args.url != '/pages/login/login') {
			if (uni.getStorageSync("token") == '' || uni.getStorageSync("token") == null) {
				uni.setStorageSync("argsUrl", args.url);
				args.url = '/pages/login/login';
			}
		}
	}
})